package com.fenghuajueli.idiomppp.db;

import android.content.Context;

import androidx.room.Room;


import com.fenghuajueli.idiomppp.utils.FileUtil;

import java.io.File;

public class DatabaseManager {
    private CaimiDatabase caimiDatabase;
    private volatile static DatabaseManager instance;

    public static DatabaseManager getInstance(Context context) {
        if (instance == null) {
            synchronized (DatabaseManager.class) {
                if (instance == null) {
                    instance = new DatabaseManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    private DatabaseManager(Context context) {
        File caimiFile = new File(context.getFilesDir(), "caimi1.db");
        if (!caimiFile.exists() || !caimiFile.isFile()) {
            FileUtil.copyFile2FileFromAssets(context, "caimi/caimi.db", caimiFile.getAbsolutePath());
        }

        caimiDatabase = Room.databaseBuilder(context, CaimiDatabase.class, caimiFile.getAbsolutePath())
                .fallbackToDestructiveMigration()
                .build();

    }

    public CaimiDatabase getCaimiDatabase() {
        return caimiDatabase;
    }
}